package cn.qingyun.gis.modules.baogan.service;

import cn.qingyun.gis.modules.baogan.entity.MicroStreetData;
import cn.qingyun.gis.modules.baogan.vo.request.ReviewRequest;
import cn.qingyun.gis.modules.init.system.vo.LoginUser;
import cn.qingyun.gis.modules.init.utils.SystemUtil;
import cn.qingyun.gis.modules.scenes.contant.ReviewEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.Objects;

import cn.qingyun.gis.modules.baogan.mapper.MicroStreetAuditLogMapper;
import cn.qingyun.gis.modules.baogan.entity.MicroStreetAuditLog;
import cn.qingyun.gis.modules.baogan.service.impl.MicroStreetAuditLogService;

import javax.annotation.Resource;

@Service
public class MicroStreetAuditLogServiceImpl extends ServiceImpl<MicroStreetAuditLogMapper, MicroStreetAuditLog> implements MicroStreetAuditLogService {

    @Resource
    private MicroStreetAuditLogMapper microStreetAuditLogMapper;
    @Resource
    private MicroStreetDataService microStreetDataService;

    @Override
    public Integer review(ReviewRequest request) {
        Integer reviewState = request.getReviewState();
        LoginUser currentUser = SystemUtil.getCurrentUser();
        if (Objects.equals(ReviewEnum.PASS.getCode(), reviewState)) {//通过
            MicroStreetAuditLog oldLog = microStreetAuditLogMapper.selectOneById(request.getId());

            //更新基础数据表
            String wktString = oldLog.getNewWkt();
            MicroStreetData streetData = new MicroStreetData();
//                Geometry geometry = new WKTReader().read(wktString);
            streetData.setWkt(wktString);
            streetData.setId(oldLog.getStreetId());
            streetData.setUpdateTime(oldLog.getCreateTime());
            streetData.setUpdateBy(oldLog.getCreateBy());
            microStreetDataService.updateWKT(streetData);
        }
        //更新审核表
        MicroStreetAuditLog auditLog = MicroStreetAuditLog.builder()
                .reviewer(currentUser.getUsername())
                .reviewRemark(request.getReviewRemark())
                .reviewState(reviewState)
                .reviewTime(new Date())
                .streetId(request.getId())
                .reviewPhone(currentUser.getPhone())
                .build();
        QueryWrapper<MicroStreetAuditLog> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MicroStreetAuditLog::getStreetId, request.getId())
                .eq(MicroStreetAuditLog::getReviewState, ReviewEnum.REVIEWED.getCode());
        return microStreetAuditLogMapper.update(auditLog, wrapper);
    }

    @Override
    public boolean adjustBoundary(MicroStreetAuditLog record) {
        return this.baseMapper.adjustBoundary(record);
    }
}
